package com.clt.ess.service.impl;


import com.clt.ess.dao.ISealImgDao;
import com.clt.ess.entity.SealApply;
import com.clt.ess.entity.SealImg;
import com.clt.ess.service.ISealImgService;
import com.clt.ess.utils.Base64Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import static com.clt.ess.utils.StringUtils.getDecryptString;
import static com.clt.ess.utils.StringUtils.getEncryptString;
import static com.clt.ess.utils.uuidUtil.getEssUUID;

@Service
public class SealImgServiceImpl implements ISealImgService {

    @Autowired
    private ISealImgDao sealImgDao;
    @Override
    public String addSealImg(SealImg sealImg) {

        //执行add动作，获取返回结果
        int result = sealImgDao.addSealImg(EncryptSealImg(sealImg));

        DecryptSealImg(sealImg);
        if(result!=0){
            return sealImg.getSealImgId();
        }else{
            return null;
        }
    }

    @Override
    public SealImg findSealImgById(String imgId) {
        return DecryptSealImg(sealImgDao.findSealImgById(imgId));
    }

    @Override
    public boolean updateSealImg(SealImg sealImg) {
        int result = sealImgDao.updateSealImg(EncryptSealImg(sealImg));
        DecryptSealImg(sealImg);
        if(result==1){
            return true;
        }
        return false;
    }

    @Override
    public SealImg createSealImg(SealApply sealApply, MultipartFile gifImg,int imgWidth,int imgHeight) throws IOException {
        SealImg sealImg = new SealImg();
        //根据上传的图像生成印章图片
        sealImg.setSealImgId(getEssUUID(sealApply.getUnitId()));
        if(gifImg!=null){
//            //将b作为输入流；
//            ByteArrayInputStream in = new ByteArrayInputStream(gifImg.getBytes());
//            //将in作为输入流，读取图片存入image中，而这里in可以为ByteArrayInputStream();
//            BufferedImage image = ImageIO.read(in);
//            int imageWidth = image.getWidth();
            sealImg.setSealImageH(imgWidth);
            sealImg.setSealImageW(imgHeight);
            sealImg.setSealImgGifBase64(Base64Utils.ESSGetBase64Encode(gifImg.getBytes()));
            sealImg.setSealImageType("png");
        }else{
            return null;
        }
        return sealImg;
    }



    @Override
    public boolean deleteSealImgById(String seaImgId) {
        return sealImgDao.deleteSealImgById(seaImgId);
    }



    private SealImg EncryptSealImg(SealImg sealImg){
        if (sealImg==null){
            return null;
        }

        if (sealImg.getSealImgGifBase64() != null){
            sealImg.setSealImgGifBase64(getEncryptString(sealImg.getSealImgGifBase64()));
        }

        return sealImg;
    }

    private SealImg DecryptSealImg(SealImg sealImg){

        if (sealImg==null){
            return null;
        }

        if (sealImg.getSealImgGifBase64()!= null){
            sealImg.setSealImgGifBase64(getDecryptString(sealImg.getSealImgGifBase64()));
        }

        return sealImg;
    }
}
